package ProjetoCompreJa.DAO;

import br.com.classesbasicas.Funcionario;
import ProjetoCompreJa.Interfaces.InterfaceFuncionario;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Marcos
 */
public class FuncionarioDAO implements InterfaceFuncionario {

    EntityManagerFactory emf;
    EntityManager em;

    public FuncionarioDAO() {
        emf = Persistence.createEntityManagerFactory("compreja");
        em = emf.createEntityManager();
    }

    @Override
    public void CadastrarFuncionario(Funcionario fun) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(fun);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    @Override
    public List<Funcionario> ListarFuncionarios() throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select funcionario from Funcionario funcionario");
            List<Funcionario> funcionarios = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return funcionarios;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    @Override
    public void ExcluirFuncionario(Funcionario fun) throws Exception {
        try {
            em.getTransaction().begin();
            em.remove(em.getReference(Funcionario.class, fun.getId()));
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());

        }
    }

    @Override
    public void AlterarFuncionario(Funcionario fun) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(fun);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }

    }

    public List<Funcionario> VerificarLogin(Funcionario fun) throws Exception {
        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select f from Funcionario f WHERE f.login = :login and f.senha = :senha");
            consulta.setParameter("login", fun.getLogin());
            consulta.setParameter("senha", fun.getSenha());
            List<Funcionario> funcionarios = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return funcionarios;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public void AlterarLoginSenha(Funcionario fun) throws Exception {
        try {
            em.getTransaction().begin();
            Query query = em.createQuery("UPDATE Funcionario set login = :login, senha = :senha  WHERE cpf = :cpf");
            query.setParameter("login", fun.getLogin());
            query.setParameter("cpf", fun.getCpf());
            query.setParameter("senha", fun.getSenha());
            query.executeUpdate();
            em.getTransaction().commit();
            emf.close();
            /*em.getTransaction().begin();
             Query consulta = em.createQuery("UPDATE Funcionario set login = :login" +
             " and senha = :senha" +
             " where cpf = :cpf" );
             consulta.setParameter("login", fun.getLogin());
             consulta.setParameter("senha", fun.getSenha());
             consulta.setParameter("cpf", fun.getCpf());
             em.getTransaction().commit();
             emf.close();*/

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
}
